package com.liush.srb.oss.controller;

import com.liush.srb.base.result.ResponseVo;
import com.liush.srb.oss.service.OssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @Author: liush
 * @CreateTime: 2023-03-31  18:47
 * @Description: 对象存储模块控制类
 */
@Api(tags = "对象存储模块")
//@CrossOrigin
@RestController
@RequestMapping("/api/oss/")
public class OssController {

    @Resource
    private OssService ossService;

    @ApiOperation("文件上传")
    @PostMapping("upload")
    //参数2： 上传的文件保存到 oss桶中的哪个目录下
    public ResponseVo upload(MultipartFile file,
                             @RequestParam("module") String module) {
        String path = ossService.upload(file, module);
        return ResponseVo.ok().data("path", path);
    }

    @ApiOperation("删除文件")
    @DeleteMapping("delete")
    public ResponseVo delete(@RequestParam(value = "path", required = true) String path) {
        ossService.deleteFile(path);
        return ResponseVo.ok().message("删除成功");
    }
}
